import { createRouter, createWebHistory } from "vue-router";
import LocalCache from "@/utils/storage";
import type { RouteRecordRaw } from "vue-router";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/ces",
    name: "Ces",
    component: () => import("@/views/ces/index.vue"),
  },
  {
    path: "/student",
    name: "Student",
    children: [
      {
        path: "addLeave",
        name: "AddLeave",
        component: () => import("@/views/student/addLeave.vue"),
      },
      {
        path: "leaveRecord",
        name: "LeaveRecord",
        component: () => import("@/views/student/leaveRecord.vue"),
      },
    ],
  },
  {
    path: "/teacher",
    name: "Teacher",
    children: [
      {
        path: "approveLeave",
        name: "ApproveLeave",
        component: () => import("@/views/teacher/approveLeave.vue"),
      },
      {
        path: "approveLeaveRecode",
        name: "ApproveLeaveRecode",
        component: () => import("@/views/teacher/approveLeaveRecode.vue"),
      },
      {
        path: "noReturnRecord",
        name: "NoReturnRecord",
        component: () => import("@/views/teacher/noReturnRecord.vue"),
      },
      {
        path: "whiteRecord",
        name: "WhiteRecord",
        component: () => import("@/views/teacher/whiteRecord.vue"),
      },
      {
        path: "whiteRecordControl",
        name: "WhiteRecordControl",
        component: () => import("@/views/teacher/whiteRecordControl.vue"),
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_PUBLIC_PATH),
  routes,
});

// 路由加载前
// router.beforeEach((to, from, next) => {
//   const token = LocalCache.get("token");
//   if (to.path === "/login" && !token) {
//     next();
//   } else {
//     if (!token) {
//       LocalCache.clear();
//       next(`/login`);
//     } else if (token && to.path === "/login") {
//       next("/index");
//     } else {
//       next();
//     }
//   }
// });

export default router;
